<?php
/**
 * @Created on 2021/05/25
 * @Author Qi
*/
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Account extends Base{

    //账号管理
    public function index(){

        $where = array();
        $name=request()->param('name');
        if($name){
            $where['name'] = array('like',"%{$name}%");
        }
        $a_id=request()->param('a_id');
        if($a_id){
            $where['a_id'] = array('like',"%{$a_id}%");
        }
        $type=request()->param('type');
        if($type){
            $where['push_type'] = $type;
        }
        $ader=request()->param('ader');
        if($ader){
            $where['ader_id'] = $ader;
        }
        $agent=request()->param('agent');
        if($agent){
            $where['agent_id'] = $agent;
        }
        $m_id=request()->param('m_id');
        if($m_id){
            $where['m_id'] = $m_id;
        }
        $start=request()->param('start');
        if($start){
            $where['open_time'] = array('egt',strtotime($start));
        }
        $end=request()->param('end');
        if($end){
            $where['open_time'] = array('elt',strtotime($end));
        }
        if(!empty($start) && !empty($end)){
            $where['open_time'] = array('between time',["{$start}","{$end}"]);
        }
        //筛选下拉
        $adlist=db('ader')->order('id desc')->select();
        $aglist=db('agent')->field('id,name')->where('status',1)->order('id desc')->select();
        $memlist=db('member')->field('uid,username')->where('roleid',2)->order('uid desc')->select();
        $list=db('account')->where($where)->order('id desc')->paginate(50,false,['query'=>request()->param()]);
        $this->assign('list',$list);
        $this->assign('name',$name);
        $this->assign('a_id',$a_id);
        $this->assign('type',$type);
        $this->assign('ader',$ader);
        $this->assign('agent',$agent);
        $this->assign('m_id',$m_id);
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('adlist',$adlist);
        $this->assign('aglist',$aglist);
        $this->assign('memlist',$memlist);
        return $this->fetch();
    }

    //添加
    public function add(){

        if(request()->isPost()){
            $name = trim(request()->param('name'));
            $a_id = trim(request()->param('a_id'));
            $m_id = trim(request()->param('m_id'));
            $ader_id = trim(request()->param('ader_id'));
            $agent_id = trim(request()->param('agent_id'));
            $push_type = trim(request()->param('push_type'));
            $rebate = trim(request()->param('rebate'));
            $init_money = trim(request()->param('init_money'));
            $open_time = trim(request()->param('open_time'));
            $status = trim(request()->param('status'));
            $data['name'] = $name;
            $data['a_id'] = $a_id;
            $data['m_id'] = $m_id;
            $data['ader_id'] = $ader_id;
            $data['agent_id'] = $agent_id;
            $data['push_type'] = $push_type;
            $data['rebate'] = $rebate;
            $data['init_money'] = $init_money;
            $data['open_time'] = strtotime($open_time);
            $data['status'] = $status;
            $data['add_time'] = time();
            $data['do_user'] = session('username');
            $data['do_id'] = session('uid');
            $true=db('account')->insert($data);
            if($true){
                system_log('account','添加推广账号');
                return json(['status'=>1,'message'=>'添加成功']);
            }else{
                return json(['status'=>0,'message'=>'添加失败']);
            }
        }else{
            $adlist=db('ader')->order('id desc')->select();
            $aglist=db('agent')->field('id,name')->where('status',1)->order('id desc')->select();
            $memlist=db('member')->field('uid,username')->where('roleid',2)->order('uid desc')->select();
            $this->assign('adlist',$adlist);
            $this->assign('aglist',$aglist);
            $this->assign('memlist',$memlist);
            return $this->fetch();
        }
    }

    //修改
    public function edit(){
        if(request()->isPost()){
            $id = request()->param('id');
            $name = trim(request()->param('name'));
            $a_id = trim(request()->param('a_id'));
            $m_id = trim(request()->param('m_id'));
            $ader_id = trim(request()->param('ader_id'));
            $agent_id = trim(request()->param('agent_id'));
            $push_type = trim(request()->param('push_type'));
            $rebate = trim(request()->param('rebate'));
            $init_money = trim(request()->param('init_money'));
            $open_time = trim(request()->param('open_time'));
            $status = trim(request()->param('status'));
            $data['name'] = $name;
            $data['a_id'] = $a_id;
            $data['m_id'] = $m_id;
            $data['ader_id'] = $ader_id;
            $data['agent_id'] = $agent_id;
            $data['push_type'] = $push_type;
            $data['rebate'] = $rebate;
            $data['init_money'] = $init_money;
            $data['open_time'] = strtotime($open_time);
            $data['status'] = $status;
            $data['do_user'] = session('username');
            $data['do_id'] = session('uid');
            $true=db('account')->where('id',$id)->update($data);
            if($true){
                system_log('account','修改推广账号');
                return json(['status'=>1,'message'=>'修改成功']);
            }else{
                return json(['status'=>0,'message'=>'修改失败']);
            }
        }else{
            $id=request()->param('id');
            $info=db('account')->where('id',$id)->find();
            $this->assign('info',$info);
            $adlist=db('ader')->order('id desc')->select();
            $aglist=db('agent')->field('id,name')->where('status',1)->order('id desc')->select();
            $memlist=db('member')->field('uid,username')->where('roleid',2)->order('uid desc')->select();
            $this->assign('adlist',$adlist);
            $this->assign('aglist',$aglist);
            $this->assign('memlist',$memlist);
            return $this->fetch();
        }
    }

    //删除
    public function del(){
        $id=request()->param('id');
        $true=db('account')->where('id',$id)->delete();
        if($true){
            system_log('account','删除推广账号');
            return json(['status'=>1,'message'=>'删除成功']);
        }else{
            return json(['status'=>0,'message'=>'删除失败']);
        }
    }
    public function addExcel()
    {
        if(request()->isPost()){
//            dump($_POST);
//            dump($_FILES);
            $files=request()->file('files');
//            dump($files);
            if(!is_dir("./data/Excel")){
                mkdir("./data/Excel");
                chmod("./data/Excel",777);
            }
            $info=$files->move(ROOT_PATH."/data/Excel");
            $data=array();
            $header=['e_date','ader','account','a_id','shows','hits','comsum','ctr','cpc','all_consum','day_consum','balance'];
            $file=$info->getInfo();
            $filename=pathinfo($info->getFilename());
            $pathinfo=pathinfo($file['name']);
            $extension=strtolower($pathinfo['extension']);
            if($extension!="xls" && $extension!="xlsx"){
                return json(['code'=>2,'msg'=>"文件格式不正确"]);
            }
            if($extension=="xlsx"){
                $type="Excel2007";
            }else{
                $type="Excel5";
            }
            $filepath='./data/Excel/';
            $filepath.=str_replace("\\","/",$info->getSaveName());
            require_once "./extend/phpexcel/PHPExcel.php";
            include "./extend/phpexcel/PHPExcel/Writer/".$type.".php";
            require_once "./extend/phpexcel/PHPExcel/IOFactory.php";
            require_once "./extend/phpexcel/PHPExcel/Shared/Date.php";
            $objReader=\PHPExcel_IOFactory::createReader($type);
            $excel=$objReader->load($filepath,"utf-8");
            $sheet=$excel->getSheet(0);
            $rows=$sheet->getHighestRow();
            $cols=$sheet->getHighestColumn();
            $dateBill=new \PHPExcel_Shared_Date();
            $all=0;
            $end=0;
            for ($currentRow = 2; $currentRow <= $rows; $currentRow++) {
                $tmp=array();
                $log=array();
                $i=0;
                for ($currentColumn = 'A'; $currentColumn <= $cols; $currentColumn++) {
                    $val=$sheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue();
                    if($val){
                        $tmp[$header[$i]]=$val;
                    }
                    $i++;
                }
                $all+=1;
                $tmp['e_date']=$dateBill->ExcelToPHP($tmp['e_date'],true,true);
                $tmp['e_date']=date("Y-m-d",$tmp['e_date']);
                $log['keys']=$all;
                $log['name']=$filename['filename'];
                $log['remark']="导入成功";
                $log['add_time']=time();
                //添加数据到表
                $tmp['add_time']=time();
                /*if($tmp['ader']!=$ader['ader']){
                    $log['remark']="广告商不匹配导入失败";
                    db('input_log')->insert($log);
                    continue;
                }*/
                $account=db('account')->where('name',$tmp['account'])->find();
                if(!$account){
                    $log['remark']="账户名不存在导入失败";
                    db('input_log')->insert($log);
                    continue;
                }
                $ader=db('ader')->where('ader',$tmp['ader'])->find();
                if(!$ader){
                    $log['remark']="广告商不存在导入失败";
                    db('input_log')->insert($log);
                    continue;
                }
                $tmp['account_id']=$account['id'];
                $tmp['ader_id']=$ader['id'];
                $tmp['m_id']=session('uid');
                $tmp['file_path']=$filepath;
                $tmp['file_name']=$filename['filename'];
                $lv=1+$account['rebate']/100;
                $tmp['daymoney']=round($tmp['day_consum']/$lv,2);
                $tmp['arebate']=$account['rebate'];
                $project=db('project')->where('id',$account['pro_id'])->field('rebate')->find();
                $tmp['prebate']=$project['rebate'];
                $tmp['dayprofit']=round((($account['rebate']-$project['rebate'])/100)*$tmp['daymoney'],2);
                $res=db('account_excel')->insert($tmp);
                if($res){
                    $end+=1;
                    db('input_log')->insert($log);
                }
            }
            system_log('account_excel',"导入用户表格-".$filename['filename']);
            return json(['code'=>1,'msg'=>"成功","data"=>['all'=>$all,'end'=>$end]]);

        }else{
            $ader=db('ader')->select();
//        $agent=db('agent')->where('status',1)->select();
            $this->assign('ader',$ader);
            return $this->fetch('addexcel');
        }

    }

    public function looksExcel()
    {
        $n=input('n')?input('n'):date('n');
        $t=input('t');
        $y=input('y')?input('y'):date("Y");
        if($n==12 && $t==2){
            $y+=1;
            $n=1;
        }elseif($n==1 && $t==1){
            $y-=1;
            $n=12;
        }elseif($t==1){
            $n-=1;
        }elseif($t==2){
            $n+=1;
        }
        $start=date("Y-m-d",strtotime($y."-".$n."-1"));
        $baseInt=strtotime($start);
        $wdays=date('w',$baseInt);
        $mdays=date('t',$baseInt);
        $l_start=strtotime("-".$wdays." days",$baseInt);
        $endInt=strtotime(date("Y-m-d",strtotime(date("Y-m-".$mdays,$baseInt))+24*3600));
        $ewdays=date('w',$endInt);
        $m_id=session('uid');
        $a_id=input('a_id');
        $l_end=$ewdays==0?$endInt:strtotime("+".(7-$ewdays)." days",$endInt);
        $data=array();
        $j=0;
        for ($i=$l_start;$i<$l_end;$i=$i+24*3600){
            $j++;
            $tmp=array();
            $list=db('account_excel')
                ->where('account_id',$a_id)
                ->where('m_id',$m_id)
                ->where('add_time',">",$i)
                ->where('add_time',"<",$i+24*3600)
                ->order('id desc')
                ->find();
            if($list){
                $tmp=$list;
            }
            $tmp['daysInt']=$i;
            $t=ceil($j/7);
            $data[$t][]=$tmp;
        }
        $this->assign('n',$n);
        $this->assign('a',$a_id);
        $this->assign('y',$y);
        $this->assign('baseInt',$baseInt);
        $this->assign('endInt',$endInt);
        $this->assign('data',$data);
        return $this->fetch('looks');
    }

    public function showLogo()
    {
        $n=input('n');
        $list=db('input_log')->where('name',$n)->select();
        return json(['code'=>1,'data'=>$list]);
    }
    public function excelList()
    {
        $account_id=input('a_id');
        $list=db('account_excel')->where('account_id',$account_id)->where('m_id',session('uid'))->paginate(100);
        $this->assign('list',$list);
        return $this->fetch('excel');
    }


    //账号消费报表
    public function accdet(){

        $where = array();
        $type=request()->param('type');
        if($type){
            $where['type'] = $type;
        }
        $start=request()->param('start');
        if($start){
            $where['e_date'] = array('egt',$start);
        }
        $end=request()->param('end');
        if($end){
            $where['e_date'] = array('elt',$end);
        }
        if(!empty($start) && !empty($end)){
            $where['e_date'] = array('between time',["{$start}","{$end}"]);
        }
        $list=db('account_detail')->where($where)->order('id desc')->paginate(50,false,['query'=>request()->param()]);
        $this->assign('list',$list);
        $this->assign('type',$type);
        $this->assign('start',$start);
        $this->assign('end',$end);
        return $this->fetch();
    }

    //添加账号充值退款记录
    public function addrecharge(){

        if(request()->isPost()){
            $accid = trim(request()->param('accid'));
            $type = trim(request()->param('type'));
            $coin = trim(request()->param('coin'));
            $money = trim(request()->param('money'));
            $e_date = trim(request()->param('e_date'));
            $compayacc = trim(request()->param('compayacc'));
            $remarks = trim(request()->param('remarks'));
            $data['accid'] = $accid;
            $data['accname'] = db('account')->where(['id'=>$accid])->value('name');
            $data['type'] = $type;
            $data['coin'] = $coin;
            $data['money'] = $money;
            $data['e_date'] = $e_date;
            $data['compayacc'] = $compayacc;
            $data['remarks'] = $remarks;
            $data['add_time'] = time();
            $data['do_user'] = session('username');
            $data['do_id'] = session('uid');
            $true=db('account_detail')->insert($data);
            if($true){
                //将报表明细中充值字段同步
                if($type == 2){
                    db('account_excel')->where(['e_date'=>$e_date,'account_id'=>$accid])->update(['recharge'=>$money]);
                }
                system_log('account_detail','添加账号充值退款记录');
                return json(['status'=>1,'message'=>'添加成功']);
            }else{
                return json(['status'=>0,'message'=>'添加失败']);
            }
        }else{
            $acclist=db('account')->field('id,name')->order('id desc')->select();
            $this->assign('acclist',$acclist);
            return $this->fetch();
        }
    }

    //账号余额
    public function accbalance(){

        $where = array();
        $name=request()->param('name');
        if($name){
            $where['name'] = array('like',"%{$name}%");
        }
        $ader=request()->param('ader');
        if($ader){
            $where['ader_id'] = $ader;
        }
        $start=request()->param('start');
        if($start){
            $where['open_time'] = array('egt',strtotime($start));
        }
        $end=request()->param('end');
        if($end){
            $where['open_time'] = array('elt',strtotime($end));
        }
        if(!empty($start) && !empty($end)){
            $where['open_time'] = array('between time',["{$start}","{$end}"]);
        }
        $adlist=db('ader')->order('id desc')->select();
        $list=db('account')->where($where)->order('id desc')->paginate(50,false,['query'=>request()->param()]);
        $this->assign('list',$list);
        $this->assign('name',$name);
        $this->assign('ader',$ader);
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('adlist',$adlist);
        return $this->fetch();
    }


}